package com.cloudlibrary.mapper;

import com.cloudlibrary.domain.Book;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;


@Mapper
public interface BookMapper {
    @Select("SELECT * FROM book WHERE book_status != 3 order by book_uploadtime desc")
    @Results(id = "bookMap", value = {
            @Result(id = true, column = "book_id", property = "id"),
            @Result(column = "book_name", property = "name"),
            @Result(column = "book_isbn", property = "isbn"),
            @Result(column = "book_press", property = "press"),
            @Result(column = "book_author", property = "author"),
            @Result(column = "book_pagination", property = "pagination"),
            @Result(column = "book_price", property = "price"),
            @Result(column = "book_uploadtime", property = "uploadtime"),
            @Result(column = "book_status", property = "status"),
            @Result(column = "book_borrower", property = "borrower"),
            @Result(column = "book_borrowtime", property = "borrowtime"),
            @Result(column = "book_returntime", property = "returntime")
    })
    Page<Book> selectBookById();

    @Select("select * from book where book_id = #{id}")
    @ResultMap("bookMap")
    Book findById(String id);

    @Update({
            "<script>",
            "update book ",
            "<trim prefix=\"set\" suffixOverrides=\",\">",
            "<if test=\"name != null\">",
            "book_name= #{name},",
            "</if>",
            "<if test=\"isbn != null\">",
            "book_isbn= #{isbn},",
            "</if>",
            "<if test=\"press != null\">",
            "book_press= #{press},",
            "</if>",
            "<if test=\"author != null\">",
            "book_author= #{author},",
            "</if>",
            "<if test=\"pagination != null\">",
            "book_pagination= #{pagination},",
            "</if>",
            "<if test=\"price != null\">",
            "book_price= #{price},",
            "</if>",
            "<if test=\"borrower != null\">",
            "book_borrower= #{borrower},",
            "</if>",
            "<if test=\"uploadtime != null\">",
            "book_uploadtime= #{uploadtime},",
            "</if>",
            "<if test=\"status != null\">",
            "book_status= #{status},",
            "</if>",
            "<if test=\"borrowtime != null\">",
            "book_borrowtime= #{borrowtime},",
            "</if>",
            "<if test=\"returntime != null\">",
            "book_returntime= #{returntime},",
            "</if>",
            "</trim>",
            "where book_id = #{id}",
            "</script>"
    })
    @ResultMap("bookMap")
    Integer editBook(Book book);


    @Select({"<script>" +
            "select * from book " +
            "where book_status !='3' " +
            "<if test=\"name != null\"> AND book_name like CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like CONCAT('%',#{press},'%')</if>" +
            "<if test=\"author != null\"> AND book_author like CONCAT('%',#{author},'%')</if>" +
            "order by book_status" +
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> searchBooks(Book book);

    @Insert({"insert into book(" +
            "book_id,book_name,book_isbn,book_press,book_author,book_pagination," +
            "book_price,book_uploadtime,book_status,book_borrower,book_borrowtime,book_returntime)"+
            "values (#{id},#{name},#{isbn},#{press},#{author},#{pagination},#{price},#{uploadtime},#{status},#{borrower},#{borrowtime},#{returntime})"
    })
    @ResultMap("bookMap")
    Integer addBook(Book book);

    @Select({"<script>" +
            "select * from book " +
            "where book_borrower = #{borrower} " +
            "AND book_status = '1' " +
            "<if test=\"name != null\"> AND book_name like CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like CONCAT('%',#{press},'%')</if>" +
            "<if test=\"author != null\"> AND book_author like CONCAT('%',#{author},'%')</if>" +
            "or book_status = '2' " +
            "<if test=\"name != null\"> AND book_name like CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like CONCAT('%',#{press},'%')</if>" +
            "<if test=\"author != null\"> AND book_author like CONCAT('%',#{author},'%')</if>" +
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectBorrowed(Book book);

    @Select({
                    "<script>" +
                    "select * from book " +
                    "where book_borrower = #{borrower} " +
                    "AND book_status in('1','2') " +
                    "<if test=\"name != null\"> AND book_name like CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press != null\"> AND book_press like CONCAT('%',#{press},'%')</if>" +
                    "<if test=\"author != null\"> AND book_author like CONCAT('%',#{author},'%')</if>" +
                    "order by book_borrowtime" +
                    "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectMyBorrowed(Book book);
}




